package com.company.cloud.mall.modules.bus.producer;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.company.cloud.mall.modules.base.entity.OrderInfo;
import com.company.cloud.mall.modules.base.entity.OrderItem;
import com.company.cloud.mall.modules.base.entity.OrderRefunds;
import com.company.cloud.mall.modules.base.entity.OrderReturns;
import com.company.common.server.config.cloud.ProjectConfig;
import com.company.common.server.config.mq.constant.MQTopicKeys;
import com.company.common.server.config.mq.models.MQOrderItem;
import com.company.common.server.config.mq.models.MQOrderModel;
import com.company.common.server.config.mq.models.MQOrderRefundsModel;
import com.company.common.server.config.mq.models.MQOrderReturnsModel;
import com.company.common.server.config.mq.utils.SaasMqUtils;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.log4j.Log4j2;

/**
 * 订单(售后)生产者
 */
@Log4j2
@Service
public class ShopOrderProducer {

    @Autowired
    private ProjectConfig projectConfig;

    /**
     * 订单状态变化
     */
    public void onOrderChange(OrderInfo order, List<OrderItem> orderItemList) {
        log.debug("+++++订单状态变化MQ发送消息");
        MQOrderModel data = this.getMQOrderModel(order, orderItemList);
        SaasMqUtils.produce(MQTopicKeys.MQ_Order_Change_Topic, JSON.toJSONString(data), true, projectConfig);
    }

    private MQOrderModel getMQOrderModel(OrderInfo order, List<OrderItem> orderItemList) {
        MQOrderModel data = new MQOrderModel();
        BeanUtil.copyProperties(order, data, "items");
        orderItemList.forEach(item -> {
            MQOrderItem orderItem = new MQOrderItem();
            BeanUtil.copyProperties(item, orderItem);
            data.addItem(orderItem);
        });
        return data;
    }

    /**
     * 退款退货售后订单状态变化
     */
    public void onReturnOrderChange(OrderReturns orderReturns) {
        log.debug("+++++退款退货售后订单状态变化MQ发送消息");
        MQOrderReturnsModel data = this.getMQOrderReturnsModel(orderReturns);
        SaasMqUtils.produce(MQTopicKeys.MQ_Return_Order_Change_Topic, JSON.toJSONString(data), true, projectConfig);
    }

    private MQOrderReturnsModel getMQOrderReturnsModel(OrderReturns orderReturns) {
        MQOrderReturnsModel data = new MQOrderReturnsModel();
        BeanUtil.copyProperties(orderReturns, data);
        return data;
    }

    /**
     * 退款售后订单状态变化
     */
    public void onRefundsOrderChange(OrderRefunds orderRefunds) {
        log.debug("+++++退款售后订单状态变化MQ发送消息");
        MQOrderRefundsModel data = this.getMQOrderRefundsModel(orderRefunds);
        SaasMqUtils.produce(MQTopicKeys.MQ_Refund_Order_Change_Topic, JSON.toJSONString(data), true, projectConfig);
    }

    private MQOrderRefundsModel getMQOrderRefundsModel(OrderRefunds orderRefunds) {
        MQOrderRefundsModel data = new MQOrderRefundsModel();
        BeanUtil.copyProperties(orderRefunds, data);
        return data;
    }


}
